#include"logger.h"

#define OUTPUT_LOG_MACROS(msg_type) { \
            char buf[strlen(format)+32];\
            time_t curtime=time(NULL);\
            sprintf(buf + strftime(buf, 32, "%Y-%m-%d %H:%M:%S", localtime(&curtime)),\
                    "  [%s]:\t%s\n", msg_type, format);\
            va_list args;\
            va_start(args, format);\
            vfprintf(file, buf , args);\
            fflush(file);\
        }
/**
 *  Prints a log message
 *  using the OUTPUT_LOG_MACROS with "ERROR" parameter
 */
void log::error(const char *format, ...)
{   if (level & LOG_ERROR)
        OUTPUT_LOG_MACROS("ERROR");
}

/**
 *  Prints a log message
 *  using the OUTPUT_LOG_MACROS with "WARNING" parameter
 */
void log::warn(const char *format, ...)
{   if (level & LOG_WARN)
        OUTPUT_LOG_MACROS("WARNING");
}

/**
 *  Prints a log message
 *  using the OUTPUT_LOG_MACROS with "INFO" parameter
 */
void log::info(const char *format, ...)
{   if (level & LOG_INFO)
        OUTPUT_LOG_MACROS("INFO");
}

/**
 *  Prints a log message
 *  using the OUTPUT_LOG_MACROS with "DEBUG" parameter
 */    
void log::debug(const char *format, ...)
{   if (level & LOG_DEBUG)
        OUTPUT_LOG_MACROS("DEBUG");
}

FILE * log::file = stderr; 
int log::level = log::LOG_ERROR | log::LOG_WARN | log::LOG_INFO | log::LOG_DEBUG; 
